package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.bean.DeviceBean;
import com.mercenary.wein.screenControl.deviceControll.AndroidDevice;
import com.mercenary.wein.screenControl.service.GroupService;
import com.mercenary.wein.screenControl.ui.MainFrame;
import com.mercenary.wein.screenControl.ui.inter.ChoosedDeviceInter;
import com.mercenary.wein.screenControl.ui.inter.GroupCheckBoxOb;
import com.mercenary.wein.screenControl.ui.inter.GroupComboBoxOb;
import com.mercenary.wein.screenControl.ui.util.CommonTools;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tuxiao on 2017/3/19.
 */
public class GroupCheckBox extends JPanel implements GroupComboBoxOb ,ChoosedDeviceInter {
    private GroupCheckBoxOb ob;
    private JCheckBox[] deviceCb;
    static GroupService groupService = new GroupService();
    private Map<Integer,DeviceBean> selectedDbs = new HashMap<Integer, DeviceBean>();

    final private List<DeviceBean> deviceBeanList = new ArrayList<DeviceBean>();
    final List<AndroidDevice> choosedADList = new ArrayList<AndroidDevice>();

    public GroupCheckBox(){
        super();
    }

    public void registerObserver(GroupCheckBoxOb ob){
        this.ob = ob;
    }

    public void notifyGroupChange(){
        if(ob!=null){
            ob.refreshData(getChoosedDevices());
        }
    }

    public int selectedDeviceNum(){
        return selectedDbs.entrySet().size();
    }



    public void refreshData(long gid) {
        this.removeAll();
        selectedDbs = new HashMap<Integer, DeviceBean>();
        final List<DeviceBean> deviceBeens = groupService.getDevicesByGid(gid);
        deviceCb = new JCheckBox[deviceBeens.size()];
        for(int i=0;i<deviceBeens.size();i++){
            final DeviceBean db = deviceBeens.get(i).clone();
            final Integer index = i;
            deviceCb[i] = new JCheckBox(db.getDname());
            deviceCb[i].addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    JCheckBox j = (JCheckBox)e.getSource();
                    if(j.isSelected()){
                        selectedDbs.put(index,db);
                        notifyGroupChange();
                    }else {
                        selectedDbs.remove(index);
                        notifyGroupChange();
                    }
                }
            });
            this.add(deviceCb[i]);
        }
        this.updateUI();
    }

    public void selectAll(){
        for(int i=0;i<deviceCb.length;i++){
            if(!deviceCb[i].isSelected()){
                deviceCb[i].setSelected(true);
            }
        }
    }

    public void invertSelect(){
        for(int i=0;i<deviceCb.length;i++){
            if(!deviceCb[i].isSelected()){
                deviceCb[i].setSelected(true);
            }else {
                deviceCb[i].setSelected(false);
            }
        }
    }

    public void unSelect(){
        for(int i=0;i<deviceCb.length;i++){
            if(deviceCb[i].isSelected()){
                deviceCb[i].setSelected(false);
            }
        }
    }

    public void refreshDeviceList(){
        deviceBeanList.removeAll(deviceBeanList);
        for(Map.Entry<Integer,DeviceBean> entry:selectedDbs.entrySet()){
            deviceBeanList.add(entry.getValue());
        }
        choosedADList.removeAll(choosedADList);
        CommonTools.getAndroidDeviceByDeviceBean(MainFrame.dcdMap,deviceBeanList,choosedADList);
    }

    public List<AndroidDevice> getChoosedADevices() {
        refreshDeviceList();
        return choosedADList;

    }

    public List<DeviceBean> getChoosedDevices() {
        refreshDeviceList();
        return deviceBeanList;
    }

}
